﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using System.Data.SqlClient;

namespace BZZ.Web.blog
{
    public partial class blogDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                bindData();
                if (Request.Cookies["nickName"] != null) txtNickName.Text = Request.Cookies["nickName"].Value;
                if (Request.Cookies["email"] != null) txtEmail.Text = Request.Cookies["email"].Value;
                if (Request.Cookies["url"] != null) txtUrl.Text = Request.Cookies["url"].Value;
            }
        }

        private void bindData()
        {
            string id = Request.QueryString["id"];
            //id = "1";
            if (id != null && id != "")
            {
                #region 变量定义
                DataSet dset = new DataSet();
                BZZ.DAL.blog dal = new BZZ.DAL.blog();
                System.Collections.Generic.List<System.Data.SqlClient.SqlParameter> paraList = new List<System.Data.SqlClient.SqlParameter>();
                string dataFields = ""; //读取的字段名，空为所有
                string strWhere = "1=1"; //查询子句
                string strOrderBy = " id desc"; //排序参数，如果需要分页，则必填 
                #endregion

                BZZ.Model.blog model = new BZZ.Model.blog();
                model = (new BZZ.DAL.blog()).GetModel(int.Parse(id));
                List<BZZ.Model.blog> list = new List<BZZ.Model.blog>();
                list.Add(model);
                rptInfoList.DataSource = list;
                rptInfoList.DataBind();
                MetaTitle.Text = model.title + " - 不知者言";
                txtBlogid.Text = model.id.ToString();

                BZZ.DAL.blogComment dalComment = new BZZ.DAL.blogComment();

                List<SqlParameter> paraList2 = new List<SqlParameter>();
                paraList2.Add(new SqlParameter("@blogid", id));
                DataSet dset2 = dalComment.GetDataset("blogid=@blogid", paraList2, "");
                rptComment.DataSource = dset2;
                rptComment.DataBind();

            }
        }

        protected void btnSumit_Click(object sender, EventArgs e)
        {
            BZZ.Model.blogComment model = new BZZ.Model.blogComment();
            model.nickName = txtNickName.Text;
            model.email = txtEmail.Text;
            model.url = txtUrl.Text;
            model.commentContent = txtCommentContent.Text;
            model.blogid =Convert.ToInt32(txtBlogid.Text);
            model.pubDate = DateTime.Now;
            model.pubIp = LIB.request.GetClientIP(this.Request);
            model.status = 1;
            model.agent = Request.UserAgent;
            Response.Cookies["nickName"].Value = model.nickName;
            Response.Cookies["email"].Value = model.email;
            Response.Cookies["url"].Value = model.url;
            Response.Cookies["nickName"].Expires = DateTime.Now.AddMonths(3);
            Response.Cookies["email"].Expires = DateTime.Now.AddMonths(3);
            Response.Cookies["url"].Expires = DateTime.Now.AddMonths(3);
            if (model.nickName != "" && model.commentContent.Length > 5)
            {
                (new BZZ.DAL.blogComment()).Add(model);
                
                //LIB.JsWrite.Alert(this, "发布成功");
                bindData();
                txtCommentContent.Text = "";
                litMessage.Text="<script>alert('感谢您的回复。');</script>";
                
                //LIB.JsWrite.AlertAndGo(this, "发布成功。", this.Request.Url.ToString());
            }
            else
            {
                LIB.JsWrite.AlertAndGoback(this,"评论太短或没有用户名。");
            }
        }
    }
}
